Option Strict Off
Option Explicit On
Module D3DShaders
	
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	'
	'  Copyright (C) 2000 Microsoft Corporation.  All Rights Reserved.
	'
	'  File:       D3DShader.bas
	'  Content:    Shader constants
	'
	'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
	
	
	' destination/source parameter register type
	Public Const D3DSI_COMMENTSIZE_SHIFT As Short = 16
	Public Const D3DSI_COMMENTSIZE_MASK As Integer = &H7FFF0000
	
	
	
	'---------------------------------------------------------------------
	' Vertex Shaders
	'
	'
	'Vertex Shader Declaration
	'
	'The declaration portion of a vertex shader defines the static external
	'interface of the shader.  The information in the declaration includes:
	'
	'- Assignments of vertex shader input registers to data streams.  These
	'assignments bind a specific vertex register to a single component within a
	'vertex stream.  A vertex stream element is identified by a byte offset
	'within the stream and a type.  The type specifies the arithmetic data type
	'plus the dimensionality (1, 2, 3, or 4 values).  Stream data which is
	'less than 4 values are always expanded out to 4 values with zero or more
	'0.F values and one 1.F value.
	'
	'- Assignment of vertex shader input registers to implicit data from the
	'primitive tessellator.  This controls the loading of vertex data which is
	'not loaded from a stream, but rather is generated during primitive
	'tessellation prior to the vertex shader.
	'
	'- Loading data into the constant memory at the time a shader is set as the
	'current shader.  Each token specifies values for one or more contiguous 4
	'DWORD constant registers.  This allows the shader to update an arbitrary
	'subset of the constant memory, overwriting the device state (which
	'contains the current values of the constant memory).  Note that these
	'values can be subsequently overwritten (between DrawPrimitive calls)
	'during the time a shader is bound to a device via the
	'SetVertexShaderConstant method.
	'
	'
	'Declaration arrays are single-dimensional arrays of DWORDs composed of
	'multiple tokens each of which is one or more DWORDs.  The single-DWORD
	'token value 0xFFFFFFFF is a special token used to indicate the end of the
	'declaration array.  The single DWORD token value 0x00000000 is a NOP token
	'with is ignored during the declaration parsing.  Note that 0x00000000 is a
	'valid value for DWORDs following the first DWORD for multiple word tokens.
	'
	'[31:29] TokenType
	'    0x0 - NOP (requires all DWORD bits to be zero)
	'    0x1 - stream selector
	'    0x2 - stream data definition (map to vertex input memory)
	'    0x3 - vertex input memory from tessellator
	'    0x4 - constant memory from shader
	'    0x5 - extension
	'    0x6 - reserved
	'    0x7 - end-of-array (requires all DWORD bits to be 1)
	'
	'NOP Token (single DWORD token)
	'    [31:29] 0x0
	'    [28:00] 0x0
	'
	'Stream Selector (single DWORD token)
	'    [31:29] 0x1
	'    [28]    indicates whether this is a tessellator stream
	'    [27:04] 0x0
	'    [03:00] stream selector (0..15)
	'
	'Stream Data Definition (single DWORD token)
	'    Vertex Input Register Load
	'      [31:29] 0x2
	'      [28]    0x0
	'      [27:20] 0x0
	'      [19:16] type (dimensionality and data type)
	'      [15:04] 0x0
	'      [03:00] vertex register address (0..15)
	'    Data Skip (no register load)
	'      [31:29] 0x2
	'      [28]    0x1
	'      [27:20] 0x0
	'      [19:16] count of DWORDS to skip over (0..15)
	'      [15:00] 0x0
	'    Vertex Input Memory from Tessellator Data (single DWORD token)
	'      [31:29] 0x3
	'      [28]    indicates whether data is normals or u/v
	'      [27:24] 0x0
	'      [23:20] vertex register address (0..15)
	'      [19:16] type (dimensionality)
	'      [15:04] 0x0
	'      [03:00] vertex register address (0..15)
	'
	'Constant Memory from Shader (multiple DWORD token)
	'    [31:29] 0x4
	'    [28:25] count of 4*DWORD constants to load (0..15)
	'    [24:07] 0x0
	'    [06:00] constant memory address (0..95)
	'
	'Extension Token (single or multiple DWORD token)
	'    [31:29] 0x5
	'    [28:24] count of additional DWORDs in token (0..31)
	'    [23:00] extension-specific information
	'
	'End-of-array token (single DWORD token)
	'    [31:29] 0x7
	'    [28:00] 0x1fffffff
	'
	'The stream selector token must be immediately followed by a contiguous set of stream data definition tokens.  This token sequence fully defines that stream, including the set of elements within the stream, the order in which the elements appear, the type of each element, and the vertex register into which to load an element.
	'Streams are allowed to include data which is not loaded into a vertex register, thus allowing data which is not used for this shader to exist in the vertex stream.  This skipped data is defined only by a count of DWORDs to skip over, since the type information is irrelevant.
	'The token sequence:
	'Stream Select: stream=0
	'Stream Data Definition (Load): type=FLOAT3; register=3
	'Stream Data Definition (Load): type=FLOAT3; register=4
	'Stream Data Definition (Skip): count=2
	'Stream Data Definition (Load): type=FLOAT2; register=7
	'
	'defines stream zero to consist of 4 elements, 3 of which are loaded into registers and the fourth skipped over.  Register 3 is loaded with the first three DWORDs in each vertex interpreted as FLOAT data.  Register 4 is loaded with the 4th, 5th, and 6th DWORDs interpreted as FLOAT data.  The next two DWORDs (7th and 8th) are skipped over and not loaded into any vertex input register.   Register 7 is loaded with the 9th and 10th DWORDS interpreted as FLOAT data.
	'Placing of tokens other than NOPs between the Stream Selector and Stream Data Definition tokens is disallowed.'
	'
	'
	
	' Vertex Shader 1.0 register limits. D3D device must provide at least
	' specified number of registers
	'
	Public Const D3DVS_INPUTREG_MAX_V1_1 As Short = 16
	Public Const D3DVS_TEMPREG_MAX_V1_1 As Short = 12
	' This max required number. Device could have more registers. Check caps.
	Public Const D3DVS_CONSTREG_MAX_V1_1 As Short = 96
	Public Const D3DVS_TCRDOUTREG_MAX_V1_1 As Short = 8
	Public Const D3DVS_ADDRREG_MAX_V1_1 As Short = 1
	Public Const D3DVS_ATTROUTREG_MAX_V1_1 As Short = 2
	Public Const D3DVS_MAXINSTRUCTIONCOUNT_V1_1 As Short = 128
	
	' Pixel Shader DX8 register limits. D3D device will have at most these
	' specified number of registers
	'
	Public Const D3DPS_INPUTREG_MAX_DX8 As Short = 8
	Public Const D3DPS_TEMPREG_MAX_DX8 As Short = 8
	Public Const D3DPS_CONSTREG_MAX_DX8 As Short = 8
	Public Const D3DPS_TEXTUREREG_MAX_DX8 As Short = 8
	
	
	Enum D3DVSD_TOKENTYPE
		D3DVSD_TOKEN_NOP = 0 ' NOP or extension
		D3DVSD_TOKEN_STREAM = 1 ' stream selector
		D3DVSD_TOKEN_STREAMDATA = 2 ' stream data definition (map to vertex input memory)
		D3DVSD_TOKEN_TESSELLATOR = 3 ' vertex input memory from tessellator
		D3DVSD_TOKEN_constMEM = 4 ' global constant memory from shader
		D3DVSD_TOKEN_EXT = 5 ' extension
		D3DVSD_TOKEN_END = 7 ' end-of-array (requires all DWORD bits to be 1)
	End Enum
	
	
	'3322 2222 2222 1111 1111 11
	'1098 7654 3210 9876 5432 1098 7654 3210
	
	
	
	Public Const D3DVSD_TOKENTYPESHIFT As Short = 29
	Public Const D3DVSD_TOKENTYPEMASK As Integer = &HE0000000 '  (7 << D3DVSD_TOKENTYPESHIFT)
	
	Public Const D3DVSD_STREAMNUMBERSHIFT As Short = 0
	Public Const D3DVSD_STREAMNUMBERMASK As Integer = &HF '(0xF << D3DVSD_STREAMNUMBERSHIFT)
	
	Public Const D3DVSD_DATALOADTYPESHIFT As Short = 28
	Public Const D3DVSD_DATALOADTYPEMASK As Integer = &H10000000 '(0x1 << D3DVSD_DATALOADTYPESHIFT)
	
	Public Const D3DVSD_DATATYPESHIFT As Short = 16
	Public Const D3DVSD_DATATYPEMASK As Decimal = &HF * 2 ^ D3DVSD_DATATYPESHIFT '0xF<< D3DVSD_DATATYPESHIFT)
	
	Public Const D3DVSD_SKIPCOUNTSHIFT As Short = 16
	Public Const D3DVSD_SKIPCOUNTMASK As Decimal = &HF * 2 ^ D3DVSD_SKIPCOUNTSHIFT '(0xF << D3DVSD_SKIPCOUNTSHIFT)
	
	Public Const D3DVSD_VERTEXREGSHIFT As Short = 0
	Public Const D3DVSD_VERTEXREGMASK As Decimal = &HF * 2 ^ D3DVSD_VERTEXREGSHIFT '(0xF << D3DVSD_VERTEXREGSHIFT)
	
	Public Const D3DVSD_VERTEXREGINSHIFT As Short = 20
	Public Const D3DVSD_VERTEXREGINMASK As Decimal = &HF * 2 ^ D3DVSD_VERTEXREGINSHIFT '(0xF << D3DVSD_VERTEXREGINSHIFT)
	
	Public Const D3DVSD_CONSTCOUNTSHIFT As Short = 25
	Public Const D3DVSD_CONSTCOUNTMASK As Decimal = &HF * 2 ^ D3DVSD_CONSTCOUNTSHIFT ' (0xF << D3DVSD_global constCOUNTSHIFT)
	
	Public Const D3DVSD_CONSTADDRESSSHIFT As Short = 0
	Public Const D3DVSD_CONSTADDRESSMASK As Integer = &H7F '(0x7F << D3DVSDCONSTADDRESSSHIFT)
	
	Public Const D3DVSD_CONSTRSSHIFT As Short = 16
	Public Const D3DVSD_CONSTRSMASK As Integer = &H1FFF0000 '(0x1FFF << D3DVSDCONSTRSSHIFT)
	
	Public Const D3DVSD_EXTCOUNTSHIFT As Short = 24
	Public Const D3DVSD_EXTCOUNTMASK As Decimal = &H1F * 2 ^ D3DVSD_EXTCOUNTSHIFT '(0x1F << D3DVSD_EXTCOUNTSHIFT)
	
	Public Const D3DVSD_EXTINFOSHIFT As Short = 0
	Public Const D3DVSD_EXTINFOMASK As Integer = &HFFFFFF '(0xFFFFFF << D3DVSD_EXTINFOSHIFT)
	
	
	' bit declarations for _Type fields
	Public Const D3DVSDT_FLOAT1 As Short = 0 ' 1D float expanded to (value, 0., 0., 1.)
	Public Const D3DVSDT_FLOAT2 As Short = 1 ' 2D float expanded to (value, value, 0., 1.)
	Public Const D3DVSDT_FLOAT3 As Short = 2 ' 3D float expanded to (value, value, value, 1.)
	Public Const D3DVSDT_FLOAT4 As Short = 3 ' 4D float
	Public Const D3DVSDT_D3DCOLOR As Short = 4 ' 4D packed unsigned bytes mapped to 0. to 1. range                                    // Input is in D3DCOLOR format (ARGB) expanded to (R, G, B, A)
	Public Const D3DVSDT_UBYTE4 As Short = 5 ' 4D unsigned byte
	Public Const D3DVSDT_SHORT2 As Short = 6 ' 2D signed short expanded to (value, value, 0., 1.)
	Public Const D3DVSDT_SHORT4 As Short = 7 ' 4D signed short
	
	' assignments of vertex input registers for fixed function vertex shader
	'
	Public Const D3DVSDE_POSITION As Short = 0
	Public Const D3DVSDE_BLENDWEIGHT As Short = 1
	Public Const D3DVSDE_BLENDINDICES As Short = 2
	Public Const D3DVSDE_NORMAL As Short = 3
	Public Const D3DVSDE_PSIZE As Short = 4
	Public Const D3DVSDE_DIFFUSE As Short = 5
	Public Const D3DVSDE_SPECULAR As Short = 6
	Public Const D3DVSDE_TEXCOORD0 As Short = 7
	Public Const D3DVSDE_TEXCOORD1 As Short = 8
	Public Const D3DVSDE_TEXCOORD2 As Short = 9
	Public Const D3DVSDE_TEXCOORD3 As Short = 10
	Public Const D3DVSDE_TEXCOORD4 As Short = 11
	Public Const D3DVSDE_TEXCOORD5 As Short = 12
	Public Const D3DVSDE_TEXCOORD6 As Short = 13
	Public Const D3DVSDE_TEXCOORD7 As Short = 14
	Public Const D3DVSDE_POSITION2 As Short = 15
	Public Const D3DVSDE_NORMAL2 As Short = 16
	
	' Maximum supported number of texture coordinate sets
	Public Const D3DDP_MAXTEXCOORD As Short = 8
	
	'---------------------------------------------------------------------
	'
	' Pixel Shader (PS) & Vertex Shader (VS) Instruction Token Definition
	'
	' **** Version Token ****
	' [07:00] minor version number
	' [15:08] major version number
	' [31:16]
	'    PS 0xFFFF
	'    VS 0xFFFE
	'
	' **** End Token ****
	' [31:00] 0x0000FFFF
	'
	' **** Comment Token ****
	' [15:00] 0xFFFE
	' [30:16] DWORD Length (up to 2^15 DWORDS = 128KB)
	' [31] 0x0
	'
	' **** Instruction Token ****
	' [15:00] Opcode (D3DSIO_*)
	' [23:16] Opcode-Specific Controls
	' [30:24] Reserved 0x0
	' [31] 0x0
	'
	' **** Destination Parameter Token ****
	' [07:00] Register Number (offset in register file)
	' [15:08] Reserved 0x0
	' [19:16] Write Mask
	'   [16] Component 0 (X;Red)
	'   [17] Component 1 (Y;Green)
	'   [18] Component 2 (Z;Blue)
	'   [19] Component 3 (W;Alpha)
	' [23:20]
	'   PS Result Modifier (bias;..)
	'   VS Reserved 0x0
	' [27:24]
	'   PS Result Shift Scale (signed )
	'   VS Reserved 0x0
	' [30:28] Register Type
	'   [0x0] Temporary Register File
	'   [0x1] Reserved
	'   [0x2] Reserved
	'   [0x3]
	'      VS Address Register (reg num must be zero)
	'      PS Reserved
	'   [0x4]
	'      VS Rasterizer Output Register File
	'      PS Reserved
	'   [0x5]
	'      VS Attribute Output Register File
	'      PS Reserved
	'   [0x6]
	'      VS Texture Coordinate Register File
	'      PS Reserved
	'   [0x7] Reserved
	' [31] 0x1
	'
	' **** Source Parameter Token *****
	' [12:00] Register Number (offset in register file)
	' [14:13]
	'   VS Relative Address Register Component
	'   PS Reserved 0x0
	' [15]
	'   VS Relative Address
	'   PS Reserved 0x0
	' [23:16] Source Component Swizzle
	'   [17:16] Component 0 Swizzle
	'   [19:18] Component 1 Swizzle
	'   [21:20] Component 2 Swizzle
	'   [23:22] Component 3 Swizzle
	' [27:24] Source Modifier
	'     [0x0] None
	'     [0x1] Negate
	'     [0x2] Complement
	'     [0x3-0xf] Reserved
	' [30:28] Register Type
	'   [0x0] Temporary Register File
	'   [0x1] Input Register File
	'   [0x2] global constant Register File
	'   [0x3-0x7] Reserved
	' [31] 0x1
	'
	
	'
	' Instruction Token Bit Definitions
	'
	Public Const D3DSI_OPCODE_MASK As Integer = &HFFFF
	
	Enum D3DSHADER_INSTRUCTION_OPCODE_TYPE
		D3DSIO_NOP = 0 ' PS/VS
		D3DSIO_MOV = 1 ' PS/VS
		D3DSIO_ADD = 2 ' PS/VS
		D3DSIO_SUB = 3 ' PS
		D3DSIO_MAD = 4 ' PS/VS
		D3DSIO_MUL = 5 ' PS/VS
		D3DSIO_RCP = 6 ' VS
		D3DSIO_RSQ = 7 ' VS
		D3DSIO_DP3 = 8 ' PS/VS
		D3DSIO_DP4 = 9 ' VS
		D3DSIO_MIN = 10 ' VS
		D3DSIO_MAX = 11 ' VS
		D3DSIO_SLT = 12 ' VS
		D3DSIO_SGE = 13 ' VS
		D3DSIO_EXP = 14 ' VS
		D3DSIO_LOG = 15 ' VS
		D3DSIO_LIT = 16 ' VS
		D3DSIO_DST = 17 ' VS
		D3DSIO_LRP = 18 ' PS
		D3DSIO_FRC = 19 ' VS
		D3DSIO_M4x4 = 20 ' VS
		D3DSIO_M4x3 = 21 ' VS
		D3DSIO_M3x4 = 22 ' VS
		D3DSIO_M3x3 = 23 ' VS
		D3DSIO_M3x2 = 24 ' VS
		D3DSIO_TEXCOORD = 64 ' PS
		D3DSIO_TEXKILL = 65 ' PS
		D3DSIO_TEX = 66 ' PS
		D3DSIO_TEXBEM = 67 ' PS
		D3DSIO_TEXBEML = 68 ' PS
		D3DSIO_TEXREG2AR = 69 ' PS
		D3DSIO_TEXREG2GB = 70 ' PS
		D3DSIO_TEXM3x2PAD = 71 ' PS
		D3DSIO_TEXM3x2TEX = 72 ' PS
		D3DSIO_TEXM3x3PAD = 73 ' PS
		D3DSIO_TEXM3x3TEX = 74 ' PS
		D3DSIO_TEXM3x3DIFF = 75 ' PS
		D3DSIO_TEXM3x3SPEC = 76 ' PS
		D3DSIO_TEXM3x3VSPEC = 77 ' PS
		D3DSIO_EXPP = 78 ' VS
		D3DSIO_LOGP = 79 ' VS
		D3DSIO_CND = 80 ' PS
		D3DSIO_DEF = 81 ' PS
		D3DSIO_COMMENT = &HFFFE
		D3DSIO_END = &HFFFF
	End Enum
	
	'
	' Co-Issue Instruction Modifier - if set then this instruction is to be
	' issued in parallel with the previous instruction(s) for which this bit
	' is not set.
	'
	Public Const D3DSI_COISSUE As Integer = &H40000000
	
	
	'
	' Parameter Token Bit Definitions
	'
	Public Const D3DSP_REGNUM_MASK As Integer = &HFFF
	
	' destination parameter write mask
	Public Const D3DSP_WRITEMASK_0 As Integer = &H10000 ' Component 0 (X;Red)
	Public Const D3DSP_WRITEMASK_1 As Integer = &H20000 ' Component 1 (Y;Green)
	Public Const D3DSP_WRITEMASK_2 As Integer = &H40000 ' Component 2 (Z;Blue)
	Public Const D3DSP_WRITEMASK_3 As Integer = &H80000 ' Component 3 (W;Alpha)
	Public Const D3DSP_WRITEMASK_ALL As Integer = &HF0000 ' All Components
	
	' destination parameter modifiers
	Public Const D3DSP_DSTMOD_SHIFT As Short = 20
	Public Const D3DSP_DSTMOD_MASK As Integer = &HF00000
	
	Enum D3DSHADER_PARAM_DSTMOD_TYPE
		D3DSPDM_NONE = 0 * 2 ^ D3DSP_DSTMOD_SHIFT '0<<D3DSP_DSTMOD_SHIFT, ' nop
		D3DSPDM_SATURATE = 1 * 2 ^ D3DSP_DSTMOD_SHIFT '1<<D3DSP_DSTMOD_SHIFT, 'clamp 0 to 1 range
	End Enum
	
	' destination parameter
	Public Const D3DSP_DSTSHIFT_SHIFT As Short = 24
	Public Const D3DSP_DSTSHIFT_MASK As Integer = &HF000000
	
	' destination/source parameter register type
	Public Const D3DSP_REGTYPE_SHIFT As Short = 28
	Public Const D3DSP_REGTYPE_MASK As Integer = &H70000000
	
	Public Const D3DVSD_STREAMTESSSHIFT As Short = 28
	Public Const D3DVSD_STREAMTESSMASK As Integer = 2 ^ D3DVSD_STREAMTESSSHIFT '(1 << D3DVSD_STREAMTESSSHIFT)
	
	Enum D3DSHADER_PARAM_REGISTER_TYPE
		D3DSPR_TEMP = &H0 '0<<D3DSP_REGTYPE_SHIFT, ' Temporary Register File
		D3DSPR_INPUT = &H20000000 '1<<D3DSP_REGTYPE_SHIFT, ' Input Register File
		D3DSPR_CONST = &H40000000 '2<<D3DSP_REGTYPE_SHIFT, ' global constant Register File
		D3DSPR_ADDR = &H60000000 '3<<D3DSP_REGTYPE_SHIFT, ' Address Register (VS)
		D3DSPR_TEXTURE = &H60000000 '3<<D3DSP_REGTYPE_SHIFT, ' Texture Register File (PS)
		D3DSPR_RASTOUT = &H80000000 '4<<D3DSP_REGTYPE_SHIFT, ' Rasterizer Register File
		D3DSPR_ATTROUT = &HA0000000 '5<<D3DSP_REGTYPE_SHIFT, ' Attribute Output Register File
		D3DSPR_TEXCRDOUT = &HC0000000 '6<<D3DSP_REGTYPE_SHIFT, ' Texture Coordinate Output Register File
	End Enum
	
	' Register offsets in the Rasterizer Register File
	'
	Enum D3DVS_RASTOUT_OFFSETS
		D3DSRO_POSITION = 0
		D3DSRO_FOG = 1
		D3DSRO_POINT_SIZE = 2
	End Enum
	
	' Source operand addressing modes
	
	Public Const D3DVS_ADDRESSMODE_SHIFT As Short = 13
	Public Const D3DVS_ADDRESSMODE_MASK As Integer = (2 ^ D3DVS_ADDRESSMODE_SHIFT)
	
	Enum D3DVS_ADRRESSMODE_TYPE
		D3DVS_ADDRMODE_ABSOLUTE = 0 '(0 << D3DVS_ADDRESSMODE_SHIFT),
		D3DVS_ADDRMODE_RELATIVE = 2 ^ D3DVS_ADDRESSMODE_SHIFT '(1 << D3DVS_ADDRESSMODE_SHIFT)   ' Relative to register A0
	End Enum
	
	' Source operand swizzle definitions
	'
	Public Const D3DVS_SWIZZLE_SHIFT As Short = 16
	Public Const D3DVS_SWIZZLE_MASK As Integer = &HFF0000
	
	' The following bits define where to take component X:
	
	Public Const D3DVS_X_X As Integer = (0 * 2 ^ D3DVS_SWIZZLE_SHIFT) '(0 << D3DVS_SWIZZLE_SHIFT)
	Public Const D3DVS_X_Y As Integer = (1 * 2 ^ D3DVS_SWIZZLE_SHIFT) '(1 << D3DVS_SWIZZLE_SHIFT)
	Public Const D3DVS_X_Z As Integer = (2 * 2 ^ D3DVS_SWIZZLE_SHIFT) '(2 << D3DVS_SWIZZLE_SHIFT)
	Public Const D3DVS_X_W As Integer = (3 * 2 ^ D3DVS_SWIZZLE_SHIFT) '(3 << D3DVS_SWIZZLE_SHIFT)
	
	' The following bits define where to take component Y:
	
	Public Const D3DVS_Y_X As Decimal = (0 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 2)) '  (0 << (D3DVS_SWIZZLE_SHIFT + 2))
	Public Const D3DVS_Y_Y As Decimal = (1 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 2)) ' (1 << (D3DVS_SWIZZLE_SHIFT + 2))
	Public Const D3DVS_Y_Z As Decimal = (2 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 2)) ' (2 << (D3DVS_SWIZZLE_SHIFT + 2))
	Public Const D3DVS_Y_W As Decimal = (3 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 2)) ' (3 << (D3DVS_SWIZZLE_SHIFT + 2))
	
	' The following bits define where to take component Z:
	
	Public Const D3DVS_Z_X As Decimal = (0 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 4)) '(0 << (D3DVS_SWIZZLE_SHIFT + 4))
	Public Const D3DVS_Z_Y As Decimal = (1 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 4)) '(1 << (D3DVS_SWIZZLE_SHIFT + 4))
	Public Const D3DVS_Z_Z As Decimal = (2 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 4)) '(2 << (D3DVS_SWIZZLE_SHIFT + 4))
	Public Const D3DVS_Z_W As Decimal = (3 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 4)) '(3 << (D3DVS_SWIZZLE_SHIFT + 4))
	
	' The following bits define where to take component W:
	
	Public Const D3DVS_W_X As Decimal = (0 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 6)) '(0 << (D3DVS_SWIZZLE_SHIFT + 6))
	Public Const D3DVS_W_Y As Decimal = (1 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 6)) '(1 << (D3DVS_SWIZZLE_SHIFT + 6))
	Public Const D3DVS_W_Z As Decimal = (2 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 6)) '(2 << (D3DVS_SWIZZLE_SHIFT + 6))
	Public Const D3DVS_W_W As Decimal = (3 * 2 ^ (D3DVS_SWIZZLE_SHIFT + 6)) '(3 << (D3DVS_SWIZZLE_SHIFT + 6))
	
	' Value when there is no swizzle (X is taken from X, Y is taken from Y,
	' Z is taken from Z, W is taken from W
	'
	Public Const D3DVS_NOSWIZZLE As Boolean = (D3DVS_X_X Or D3DVS_Y_Y Or D3DVS_Z_Z Or D3DVS_W_W)
	
	' source parameter swizzle
	Public Const D3DSP_SWIZZLE_SHIFT As Short = 16
	Public Const D3DSP_SWIZZLE_MASK As Integer = &HFF0000
	
	Public Const D3DSP_NOSWIZZLE As Boolean = ((0 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 0)) Or (1 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 2)) Or (2 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 4)) Or (3 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 6)))
	
	' pixel-shader swizzle ops
	Public Const D3DSP_REPLICATEALPHA As Boolean = ((3 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 0)) Or (3 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 2)) Or (3 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 4)) Or (3 * 2 ^ (D3DSP_SWIZZLE_SHIFT + 6)))
	
	
	' source parameter modifiers
	Public Const D3DSP_SRCMOD_SHIFT As Short = 24
	Public Const D3DSP_SRCMOD_MASK As Integer = &HF000000
	
	Enum D3DSHADER_PARAM_SRCMOD_TYPE
		D3DSPSM_NONE = 0 * 2 ^ D3DSP_SRCMOD_SHIFT '0<<D3DSP_SRCMOD_SHIFT, ' nop
		D3DSPSM_NEG = 1 * 2 ^ D3DSP_SRCMOD_SHIFT ' negate
		D3DSPSM_BIAS = 2 * 2 ^ D3DSP_SRCMOD_SHIFT ' bias
		D3DSPSM_BIASNEG = 3 * 2 ^ D3DSP_SRCMOD_SHIFT ' bias and negate
		D3DSPSM_SIGN = 4 * 2 ^ D3DSP_SRCMOD_SHIFT ' sign
		D3DSPSM_SIGNNEG = 5 * 2 ^ D3DSP_SRCMOD_SHIFT ' sign and negate
		D3DSPSM_COMP = 6 * 2 ^ D3DSP_SRCMOD_SHIFT ' complement
	End Enum
	
	
	
	'''''''''''''''''''''''''''''''''''
	'PIXELSHADER FUNCTIONS
	'''''''''''''''''''''''''''''''''''
	
	
	' pixel shader version token
	Function D3DPS_VERSION(ByRef Major As Integer, ByRef Minor As Integer) As Integer
		D3DPS_VERSION = (&HFFFF0000 Or ((Major) * 2 ^ 8) Or (Minor))
	End Function
	
	
	' vertex shader version token
	Function D3DVS_VERSION(ByRef Major As Integer, ByRef Minor As Integer) As Integer
		D3DVS_VERSION = (&HFFFE0000 Or ((Major) * 2 ^ 8) Or (Minor))
	End Function
	
	
	' extract major/minor from version cap
	Function D3DSHADER_VERSION_MAJOR(ByRef Version As Integer) As Integer
		D3DSHADER_VERSION_MAJOR = (((Version) \ 8) And &HFF)
	End Function
	
	Function D3DSHADER_VERSION_MINOR(ByRef Version As Integer) As Integer
		D3DSHADER_VERSION_MINOR = (((Version)) And &HFF)
	End Function
	
	
	Function D3DSHADER_COMMENT(ByRef DWordSize As Integer) As Integer
		D3DSHADER_COMMENT = ((((DWordSize) * 2 ^ D3DSI_COMMENTSIZE_SHIFT) And D3DSI_COMMENTSIZE_MASK) Or D3DSHADER_INSTRUCTION_OPCODE_TYPE.D3DSIO_COMMENT)
	End Function
	
	
	' pixel/vertex shader end token
	Function D3DPS_END() As Integer
		D3DPS_END = &HFFFF
	End Function
	
	Function D3DVS_END() As Integer
		D3DVS_END = &HFFFF
	End Function
	
	
	
	
	
	'''''''''''''''''''''''''''''''''''
	'VERTEXSHADER FUNCTIONS
	'''''''''''''''''''''''''''''''''''
	
	
	Function D3DVSD_MAKETOKENTYPE(ByRef tokenType As Integer) As Integer
		Dim out As Integer
		Select Case tokenType
			Case D3DVSD_TOKENTYPE.D3DVSD_TOKEN_NOP '0
				out = 0
			Case D3DVSD_TOKENTYPE.D3DVSD_TOKEN_STREAM '1          ' stream selector
				out = &H20000000
			Case D3DVSD_TOKENTYPE.D3DVSD_TOKEN_STREAMDATA '2      ' stream data definition (map to vertex input memory)
				out = &H40000000
			Case D3DVSD_TOKENTYPE.D3DVSD_TOKEN_TESSELLATOR ' 3     ' vertex input memory from tessellator
				out = &H60000000
			Case D3DVSD_TOKENTYPE.D3DVSD_TOKEN_constMEM '4        ' global constant memory from shader
				out = &H80000000
			Case D3DVSD_TOKENTYPE.D3DVSD_TOKEN_EXT '5             ' extension
				out = &HA0000000
			Case D3DVSD_TOKENTYPE.D3DVSD_TOKEN_END '7             ' end-of-array (requires all DWORD bits to be 1)
				out = &HFFFFFFFF
		End Select
		D3DVSD_MAKETOKENTYPE = out And D3DVSD_TOKENTYPEMASK
	End Function
	
	
	' macros for generation of CreateVertexShader Declaration token array
	
	' Set current stream
	' _StreamNumber [0..(MaxStreams-1)] stream to get data from
	'
	Function D3DVSD_STREAM(ByRef StreamNumber As Integer) As Integer
		D3DVSD_STREAM = (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKENTYPE.D3DVSD_TOKEN_STREAM) Or (StreamNumber))
	End Function
	
	
	' Set tessellator stream
	'
	Function D3DVSD_STREAM_TESS() As Integer
		D3DVSD_STREAM_TESS = (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKENTYPE.D3DVSD_TOKEN_STREAM) Or (D3DVSD_STREAMTESSMASK))
	End Function
	
	
	' bind single vertex register to vertex element from vertex stream
	'
	' VertexRegister [0..15] address of the vertex register
	' dataType [D3DVSDT_*] dimensionality and arithmetic data type
	Function D3DVSD_REG(ByRef VertexRegister As Integer, ByRef dataType As Integer) As Integer
		D3DVSD_REG = (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKENTYPE.D3DVSD_TOKEN_STREAMDATA) Or ((dataType) * 2 ^ D3DVSD_DATATYPESHIFT) Or (VertexRegister))
	End Function
	
	' Skip DWORDCount DWORDs (vb longs) in vertex
	'
	Function D3DVSD_SKIP(ByRef DWORDCount As Integer) As Integer
		D3DVSD_SKIP = (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKENTYPE.D3DVSD_TOKEN_STREAMDATA) Or &H10000000 Or ((DWORDCount) * 2 ^ D3DVSD_SKIPCOUNTSHIFT))
	End Function
	
	' load data into vertex shader global constant memory
	'
	' _constantAddress [0..95] - address of global constant array to begin filling data
	' _Count [0..15] - number of global constant vectors to load (4 DWORDs each)
	' followed by 4*_Count DWORDS of data
	'
	Function D3DVSD_CONST(ByRef constantAddress As Integer, ByRef count As Integer) As Integer
		D3DVSD_CONST = (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKENTYPE.D3DVSD_TOKEN_constMEM) Or ((count) * 2 ^ D3DVSD_CONSTCOUNTSHIFT) Or (constantAddress))
	End Function
	
	' enable tessellator generated normals
	'
	'  VertexRegisterIn  [0..15] address of vertex register whose input stream
	'                            will be used in normal computation
	'  VertexRegisterOut [0..15] address of vertex register to output the normal to
	'
	Function D3DVSD_TESSNORMAL(ByRef VertexRegisterIn As Integer, ByRef VertexRegisterOut As Integer) As Integer
		D3DVSD_TESSNORMAL = (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKENTYPE.D3DVSD_TOKEN_TESSELLATOR) Or ((VertexRegisterIn) * 2 ^ D3DVSD_VERTEXREGINSHIFT) Or ((&H2) * 2 ^ D3DVSD_DATATYPESHIFT) Or (VertexRegisterOut))
	End Function
	
	
	' enable tessellator generated surface parameters
	'
	' VertexRegister [0..15] address of vertex register to output parameters
	'
	Function D3DVSD_TESSUV(ByRef VertexRegister As Integer) As Integer
		D3DVSD_TESSUV = (D3DVSD_MAKETOKENTYPE(D3DVSD_TOKENTYPE.D3DVSD_TOKEN_TESSELLATOR) Or &H10000000 Or ((&H1) * 2 ^ D3DVSD_DATATYPESHIFT) Or (VertexRegister))
	End Function
	
	' Generates END token
	'
	Function D3DVSD_END() As Integer
		D3DVSD_END = &HFFFFFFFF
	End Function
	
	Function D3DVSD_NOP() As Integer
		D3DVSD_NOP = 0
	End Function
End Module